package com.acme.bankapp.service.bank.email;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by Alexander Levin on 21.11.2014.
 */
public class EmailQueue {
    private final Queue<Email> queue = new LinkedList<>();

    public void enqueue(final Email email) {
        synchronized (queue) {
            queue.add(email);
            queue.notify();
        }
    }

    public Email dequeue() {
        synchronized (queue) {
            while (queue.isEmpty()) {
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                    return null;
                }
            }
            return queue.remove();
        }
    }

    public boolean isEmpty() {
        return queue.isEmpty();
    }
}
